<?php
	
	function send_msg_to_goal_user($id){

		$DB_comments = M('comments');
		$DB_message = M('notification_message');
		$DB_message_log =  M('notification_message_log');
		$DB_members = M('members');

		$from_user_comment = $DB_comments->where(array('id' =>$id))->find();
		$to_user_comment = $DB_comments->where(array('id' => $from_user_comment['pid']))->find();

		$from_user_name = $DB_members->where(array('id' => $from_user_comment['mid']))->getField('username');
		$to_user_name = $DB_members->where(array('id' => $to_user_comment['mid']))->getField('username');
                
                

		//判断用户评论的项目类型
		if($from_user_comment['ctype'] == 1){

			//获取用户评论的项目名称
			$articlename = M('fundings')->where(array('id' => $from_user_comment['for_id']))->getField('pjname');

		}else{

			$articlename = M('news')->where(array('id' => $from_user_comment['for_id']))->getField('title');
		}

		$data_message['title'] = '您的评论有新的回复!';
		// $data_message['text'] = '用户'.$from_user_name.'对您在'.$articlename.'的评论【'.$to_user_comment['content'].'】进行了回复。回复内容是【'.$from_user_comment['content'].'】';
//		$data_message['text'] = '您对"'.$articlename.'"的评价,收到了 "'.$from_user_name.'" 的回复: '.$from_user_comment['content'];
                $data_message['text'] = '您对 <'.$articlename.'>的评价,收到了 <'.$from_user_name.'> 的回复: '.$from_user_comment['content'];
		$message_id = $DB_message->data($data_message)->add();

		if(!empty($message_id)){

			$data_message_log['mid'] = $to_user_comment['mid'];
			$data_message_log['message_id'] = $message_id;
			$data_message_log['ctime'] = I('server.REQUEST_TIME');

			$re = $DB_message_log->data($data_message_log)->add();
                        
                        
                        /*
                         * 微信下发消息
                         */
                        send_msg_with_wechat($to_user_comment['mid'], $data_message['text']);
		}
				
	}
        
        /*
         * 调用微信的功能下发消息
         */
        function send_msg_with_wechat($mid,$content)
        {
            $DB_members = M('members');
            $openid = $DB_members->where(array('id' => $mid))->getField('wxopenid');
            
            if(!empty($openid))
            {
                $jssdk = new \Com\JSSDK(C('appid'), C('appsecret'));
                $accessToken = $jssdk->returnAccessToken();
        
                //初始化 对象
                $wxsdk = new \Com\WechatAuth(C('appid'), C('appsecret'),$accessToken);



                $wxsdk->sendText($openid, $content);
            }
            
            
        }



	//对用户的评论进行敏感词检测
	function chk_bad_words($content,$type){

		$DB = M('bad_words_filter');

		$list = $DB->where(array('type' => $type))->getField('keywords',true);

		$ifcontain = false;
		foreach ($list as $key => $value) {
			# code...
			if(!$ifcontain)
			{
				$arr = explode($value,$content);
				if(count($arr)>1){

					// $ifcontain=true;
					$res['err']=1;
		            $res['msg']="关键字非法";
		            $res['count']=0;
		            $res['content']="";

		            echo json_encode($res);
					exit();
					
				}
				
			}
		}

	}

?>